<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input id="i1" type="text">
<input type="button" value="平方" onclick="f()">
<br>
<input id="i2" type="text">
<input type="button" value="求和" onclick="f1()">

<div></div>

<script>
    function f() {
        //得到文本框和div对象
        let i = document.querySelector("#i1");
        let d = document.querySelector("div");
        //判断用户输入的内容NaN
        if (isNaN(i.value)){
            d.innerText="输入错误";
            return;
        }
        //文本框赋值给div
        //当对字符串进行 - * /运算时会自动转成数值
        d.innerText = i.value*i.value;

    }

    function f1() {
        //得到文本框和div对象
        let i = document.querySelector("#i1");
        let d = document.querySelector("div");

        //进行加法运算需要手动将字符串转成数值，否则是字符串拼接
        //d.innerText = parseFloat(i.value) + parseFloat(i.value);
        d.innerText = i.value*1+i.value*1;
    }
</script>
</body>
</html>